package com.lnj.community.service.Impl;

import com.lnj.community.dao.entity.Message;
import com.lnj.community.mapper.MessageMapper;
import com.lnj.community.service.MessageService;
import com.lnj.community.utiles.SensitiveUtile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class MessageServerImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private SensitiveUtile sensitiveUtile;

    //查询当前用户的会话列表,针对每个会话只返回一条最新的私信.
    @Override
    public List<Message> findConversations(int userId, int offset, int limit) {
        return messageMapper.selectConversations(userId, offset, limit);
    }

    //查询当前用户的会话数量.
    @Override
    public int findConversationCount(int id) {
        return messageMapper.selectConversationCount(id);
    }

    //查询某个会话所包含的私信列表.
    @Override
    public List<Message> findLetters(String conversationId, int offset, int limit) {
        return messageMapper.selectLetters(conversationId, offset, limit);
    }

    //查询某个会话所包含的私信数量.
    @Override
    public int findLetterCount(String conversationId) {
        return messageMapper.selectLetterCount(conversationId);
    }

    //查询未读私信的数量
    @Override
    public int findLetterUnreadCount(int id, String conversationId) {
        return messageMapper.selectLetterUnreadCount(id, conversationId);
    }

    //添加消息 发送消息
    @Override
    public int addMessage(Message message) {
        //先过滤敏感词
        if (message.getContent() != null) {
            message.setContent(HtmlUtils.htmlEscape(message.getContent()));
            message.setContent(sensitiveUtile.filter(message.getContent()));
        }
        return messageMapper.insertMessage(message);
    }

    //修改消息状态
    @Override
    public int readMessage(List<Integer> ids) {
        return messageMapper.updateStatus(ids, 1);
    }

    //查询某个主题下最新的通知
    @Override
    public Message findLatestNotice(String topic, int userId) {
        return messageMapper.selectLatestNotice(userId, topic);
    }

    //查询某个主题所包含的通知数量
    @Override
    public int findNoticeCount(String topic, int userId) {
       return messageMapper.selectNoticeCount(userId, topic);
    }

    //查询未读的通知的数量
    @Override
    public int findNoticeUnreadCount(String topic, int userId) {
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }

    //查询某个主题所包含的通知列表
    @Override
    public List<Message> findNotices(String topic, int userId, int offset, int limit) {
       return messageMapper.selectNotices(userId, topic, offset, limit);
    }
}
